package {

	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;

	public class Main extends Sprite {
		private var _image:Image = new Image();
		private var _bmp1:Bitmap = new Bitmap(new BitmapData(_image.width, _image.height, true, 0));
		private var _bmp2:Bitmap = new Bitmap(new BitmapData(_image.width, _image.height, true, 0));	

		public function Main() {
			_bmp1.bitmapData.draw(_image);
			_bmp2.bitmapData.draw(_image);
			_bmp2.x = 200;
			_bmp2.y = 200;
			
			addChild(_bmp1);
			addChild(_bmp2);			

			addEventListener(Event.ENTER_FRAME, onFrame);
		}

		private function onFrame(e:Event):void {
			_bmp1.x = mouseX;
			_bmp1.y = mouseY;						
			
			_image.rotation++;
			var rect:Rectangle = _image.getRect(this);
			
			_bmp1.bitmapData = new BitmapData(_image.width, _image.height, false, 0x000000);
			var matrix:Matrix = _image.transform.matrix;
			matrix.translate(-rect.x, -rect.y);
			_bmp1.bitmapData.draw(_image, matrix);
			
			/*var realRect:Rectangle = _bmp1.bitmapData.getColorBoundsRect(0xFFFFFF, 0x000000, false);			
			var bmpd:BitmapData = new BitmapData(realRect.width, realRect.height, false, 0);
			bmpd.copyPixels(_bmp1.bitmapData, realRect, new Point(0, 0));
			_bmp1.bitmapData = bmpd;*/			
			
			_bmp1.x += rect.x + (-60*Math.cos(toRadians(_image.rotation))) + (-3*Math.sin(toRadians(_image.rotation)));
			_bmp1.y += rect.y + (-60*Math.sin(toRadians(_image.rotation))) + (-3*Math.cos(toRadians(_image.rotation)));
			
			var c:Boolean = _bmp1.bitmapData.hitTest(new Point(_bmp1.x, _bmp1.y), 255, _bmp2, new Point(_bmp2.x, _bmp2.y), 255);			

			if(c)
				_bmp2.alpha = _bmp1.alpha = 0.3;
			else
				_bmp2.alpha = _bmp1.alpha = 1;
		}
		
		private function toRadians(degrees:Number):Number {
			return degrees * Math.PI / 180;
		}
	}
}